package com.huahan.auth.security.component;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.huahan.auth.domain.User;
import com.huahan.auth.domain.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

public class HuahanUserDetail implements UserDetails, Serializable {
    @JsonSerialize
    private static final long serialVersionUID = -1L;
    private User user;
    private UserInfo appUser;
    /**
     * 权限集合
     */
    private List<GrantedAuthority> authorities;
    //private List<MenuDO> authorities;

    /**
     * 权限集合
     */

    public HuahanUserDetail() {
    }

    public HuahanUserDetail(User user,
                            List<GrantedAuthority> authorities) {
        this.user = user;
        this.authorities = authorities;
    }
    public HuahanUserDetail(UserInfo user,
                            List<GrantedAuthority> authorities) {
        this.appUser = user;
        this.authorities = authorities;
        appUser.setPhoneNumber(user.getPhoneNumber());
    }
//    public HuahanUserDetail(UserDO user,
//                            List<MenuDO> authorities) {
//        this.user = user;
//        this.authorities = authorities;
//    }
    public User getUser() {
        return user;
    }

    @Override public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override public String getPassword() {
        return getUser().getPassword();
    }

    @Override public String getUsername() {
        return getUser().getUsername();
    }

    @Override public boolean isAccountNonExpired() {
        return Boolean.TRUE;
    }

    @Override public boolean isAccountNonLocked() {
        return Boolean.TRUE;
    }

    @Override public boolean isCredentialsNonExpired() {
        return Boolean.TRUE;
    }

    @Override public boolean isEnabled() {
        return StringUtils.equals(user.getStatus().toString(), "1");
    }
}
